﻿using Janus.BaseEntitys;
using Janus.Cores.Extension;
using Janus.Cores.Linq;
using Janus.Mes.DTO;
using Janus.Mes.Services.Interface;
using Janus.Mes.VO;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;

namespace Janus.Mes.WebClient.Areas.Quality.Controllers.SampleDefin
{
    public class SampleDefinController : BaseController
    {
        // 自定义抽样表
        [HttpGet]
        public ActionResult Index()
        {
            return View(new List<CustomSampHVO>());
        }

        /// <summary>
        /// 返回查询数据
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            return View(GetList(collection));
        }

        /// <summary>
        /// 新增、修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Create(string id)
        {
            CustomSampHVO customsampHVO = null;
            ViewBag.IsCreate = true;
            if (!string.IsNullOrEmpty(id))
            {
                ViewBag.IsCreate = false;
                Querier query = new Querier(" CustSCode={@0} ", new List<object>() { id });
                var model = CustomSampHServices.FindOne(query);
                customsampHVO = model.ToEntity<CustomSampHVO>();
            }
            else
            {
                customsampHVO = base.InitModel<CustomSampHVO>(customsampHVO);
            }
            return View(customsampHVO);
        }

        #region 新增/编辑提交
        /// <summary>
        /// 新增/编辑提交
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public string Create(FormCollection form)
        {
            try
            {
                var result = false;
                CustomSampHVO model = new CustomSampHVO();
                model = base.SetModel<CustomSampHVO>(form);
                var customsamph = model.ToEntity<CustomSampHDTO>();
                result = CustomSampHServices.Save(customsamph) > 0;
                if (result)
                {
                    ajaxStr = base.AjaxResultSucess;
                }
                else
                {
                    ajaxStr = base.AjaxResultFail;
                }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = base.AjaxResultFail;
            }
            return "{" + ajaxStr + "}";
        }
        #endregion

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        public string Delete(string id)
        {
            try
            {
                var result = CustomSampHServices.Delete(id);
                if (result)
                {
                    ajaxStr = base.AjaxDeleteSucess;
                }
                else
                {
                    ajaxStr = base.AjaxResultFail;
                }
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = base.AjaxResultFail;
            }

            return "{" + ajaxStr + "}";
        }

        #region 主键是否存在
        /// <summary>
        /// 检查主键是否存在
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string IsExist()
        {
            string data = Request["data"];
            Querier query = new Querier(" CustSCode={@0} ", new List<object>() { data });
            bool result = CustomSampHServices.IsExists(query);
            if (!result)
            {
                ajaxStr = base.AjaxResultSucess;
            }
            else
            {
                ajaxStr = base.AjaxResultFail;
            }
            return "{" + ajaxStr + "}";
        }
        #endregion

        #region 私有方法
        protected IEnumerable<CustomSampHVO> GetList(FormCollection collection)
        {
            var searchParma = base.SearchPara<CustomSampHDTO>(collection);
            //if (searchParma.Model.Title.IsNotEmpty())
            //{
            //    searchParma.Queriers.Where("Title.Contains({@0})", searchParma.Model.Title);
            //}
            //可以继续判断 增加查询条件

            var result = base.FindAll<CustomSampHDTO>(searchParma, CustomSampHServices);
            if (!result.ErrorMessage.IsNotEmpty())
            {
                var list = result.Result.ToEntitys<CustomSampHVO>();// AutoMapper.Mapper.DynamicMap<List<QCParameterVO>>(result.Result);
                return list;
            }
            else
            {
                return new List<CustomSampHVO>();
            }
        }
        #endregion



        public ActionResult Select()
        {
            return View();
        }
        public ActionResult Detail()
        {
            return View();
        }
        #region 检验水平
        public ActionResult LevelmanaList(int id)
        {
            ViewBag.ID = id;
            return View();
        }
        public ActionResult AddLevelmana()
        {
            return View();
        }
        public ActionResult DeleLevelmana()
        {
            return View();
        }
        #endregion

        #region 检验参数
        public ActionResult ParamanaList()
        {
            string Sid = string.Empty;
            try
            {
                 Sid = Request["SampleDefin_id"];
            }
            catch (Exception ex)
            {
                throw ex;
            }
           
            return View();
        }
        public ActionResult AddParamana()
        {
            return View();
        }
        public ActionResult DeleParamana()
        {
            return View();
        }
        #endregion
    }
}